
Imports std
Imports System.IO

Module excludeWF

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim istream As FileStream = Nothing
        Dim reader As TextReader = Nothing
        Dim ostream As FileStream = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim i As Int32
        Dim excludes As vector(Of String) = Nothing

        If isdebugmode() Then
            pause()
        End If
        If args.Length < 2 Then
            raiseError("argument count error, input output excludeFile, " + _
                        "if input, output or excludeFile is /-, use console, program will transform backslashes", _
                        errorHandle.errorType.critical)
        Else
            If args(2) = "/-" Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(2), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            excludes = New vector(Of String)
            input = reader.ReadLine
            While Not input Is Nothing
                unescape(input)
                excludes.push_back(input)
                input = reader.ReadLine
            End While
            closestream(reader)

            If args(0) = "/-" Then
                reader = Console.In
            Else
                istream = New FileStream(args(0), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                reader = New StreamReader(istream)
            End If
            assert(Not reader Is Nothing, "file no found, " + args(0))

            If args(1) = "/-" Then
                writer = Console.Out
            Else
                ostream = New FileStream(args(1), FileMode.Create, FileAccess.Write, FileShare.Read)
                writer = New StreamWriter(ostream)
            End If
            assert(Not writer Is Nothing, "file no found, " + args(1))

            input = reader.ReadLine
            While Not input Is Nothing
                kickNewlineChar(input)
                If Not isemptystring(input) Then
                    For i = 0 To excludes.size() - 1
                        If input.Contains(excludes(i)) Then
                            Exit For
                        End If
                    Next
                End If

                If i = excludes.size() Then
                    writer.WriteLine(input)
                End If
                input = reader.ReadLine
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
